<?php
/*
 * 注明：开奖数据采集来源于API网站（pay5000.com）
 * 访问pay5000.com申请到接口后，把token填写到下
 * 方的define('TOKEN',"接口账号的token"）即可
*/
//////////////////API设置//////////////////
define('HOST', "http://api.pay5000.com");//
define('TOKEN', "接口账号的token");      //
///////////////////////////////////////////

define('Copyright', 'Copyright 2019 HK6 All Rights Reserved.');
define('ROOT_PATH', $_SERVER["DOCUMENT_ROOT"] . '/');
require("curl_http.php");
include_once ROOT_PATH . '/foot/check.php';

//获取API数据
function getDataByApi()
{
    $curl = new Curl_HTTP_Client();
    $curl->set_referrer("");
    $curl->set_user_agent("Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Maxthon/4.4.3.4000 Chrome/30.0.1599.101");
    $html = $curl->fetch_url('https://kclm.site/api/trial/drawResult?code=hk6&format=json&rows=3');
    $json = json_decode($html, true);
    $data = $json['data'];

    $result = array();
    //期号
    $result['expect'] = $data[0]['issue'];
    //号码
    $result['opencode'] = $data[0]['drawResult'];
    //时间
    $result['opentime'] = $data[0]['drawTime'];
    return $result;
}

function auto_up($data)
{
    if (empty($data['expect'])) {
        echo "获取APi数据失败!";
        exit;
    }
    $expect = $data['expect'];
    $opencode = $data['opencode'];
    $opentime = $data['opentime'];

    $arr = explode(",", $opencode);
    $num = array();
    $num['qishu'] = $expect;
    $num['date'] = $opentime;
    $num['n1'] = $arr[0];
    $num['n2'] = $arr[1];
    $num['n3'] = $arr[2];
    $num['n4'] = $arr[3];
    $num['n5'] = $arr[4];
    $num['n6'] = $arr[5];
    $num['na'] = $arr[6];

    //第一期
    $num['nq'] = $expect + 1;
    $w = date("w", strtotime($opentime));//取得一周的第几天,星期天开始0-6
    if ($w == 6) {
        $num['kaijiang'] = date('Y-m-d H:i:s', strtotime("$opentime +3 day"));
    } else {
        $num['kaijiang'] = date('Y-m-d H:i:s', strtotime("$opentime +2 day"));
    }
    $firstDay = date('Y-m-d H:i:s', strtotime($num['kaijiang']));
    $num['kaipan'] = date('Y-m-d H:i:s', strtotime("$firstDay -13 hour"));
    $num['fengpan'] = date('Y-m-d H:i:s', strtotime("$firstDay -5 minute"));
    //第二期
    $num['nq2'] = $expect + 2;
    $w2 = date("w", strtotime($num['kaijiang']));
    if ($w2 == 6) {
        $num['kaijiang2'] = date('Y-m-d H:i:s', strtotime("$firstDay +3 day"));
    } else {
        $num['kaijiang2'] = date('Y-m-d H:i:s', strtotime("$firstDay +2 day"));
    }
    $secondDay = date('Y-m-d H:i:s', strtotime($num['kaijiang2']));
    $num['kaipan2'] = date('Y-m-d H:i:s', strtotime("$secondDay -13 hour"));
    $num['fengpan2'] = date('Y-m-d H:i:s', strtotime("$secondDay -5 minute"));
    //第三期
    $num['nq3'] = $expect + 3;
    $w3 = date("w", strtotime($num['kaijiang2']));
    if ($w3 == 6) {
        $num['kaijiang3'] = date('Y-m-d H:i:s', strtotime("$secondDay +3 day"));
    } else {
        $num['kaijiang3'] = date('Y-m-d H:i:s', strtotime("$secondDay +2 day"));
    }
    $thirdDay = date('Y-m-d H:i:s', strtotime($num['kaijiang3']));
    $num['kaipan3'] = date('Y-m-d H:i:s', strtotime("$thirdDay -13 hour"));
    $num['fengpan3'] = date('Y-m-d H:i:s', strtotime("$thirdDay -5 minute"));
    /*
    echo "<br>第1期开奖时间：".$num['kaijiang'];
    echo "<br>第1期开盘时间：".$num['kaipan'];
    echo "<br>第1期封盘时间：".$num['fengpan'];
    echo "<br>";
    echo "<br>第2期开奖时间：".$num['kaijiang2'];
    echo "<br>第2期开盘时间：".$num['kaipan2'];
    echo "<br>第2期封盘时间：".$num['fengpan2'];
    echo "<br>";
    echo "<br>第3期开奖时间：".$num['kaijiang3'];
    echo "<br>第3期开盘时间：".$num['kaipan3'];
    echo "<br>第3期封盘时间：".$num['fengpan3'];
    die;
    */
    return $num;
}

?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD html 4.0 Transitional//EN">
<html>
<head>
    <title>自动开奖页面</title>
    <meta http-equiv="X-UA-Compatible" content="IE=EmulateIE8"/>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312"/>
    <link rel="stylesheet" type="text/css" href="images/dommon.css">
    <link rel="stylesheet" type="text/css" href="css/<?php global $g_stylss;
    echo $g_stylss; ?>"/>
</head>
<body class="normalBody" oncontextmenu="window.event.returnValue=false">
<div class="wrapper">
    <div class="route">
        <div>
            <span>自动开奖页面----请不要关闭</span>
        </div>
    </div><!-- route end -->
    <table class="listDetailTable">
        <tr>
            <td width=100% align=left>
                <b>
                    <?php
                    $result = getDataByApi();
                    if (empty($result['expect'])) {
                        echo "获取APi数据失败!";
                        exit;
                    }
                    $newQi = $result['expect'];
                    $autos = $db->query("SELECT `nn`,`date`,`n1`,`n2`,`n3`,`n4`,`n5`,`n6`,`na` FROM `cn_kithe` WHERE `nn` = '{$newQi}'", 1);
                    if ($autos[0]['na'] == 0) {
                        //添加新的盘口
                        $start = auto_up($result);
                        //第一期
                        $id = $db->query(" select * from `ya_kithe` where id='1'", 1);
                        if (empty($id)) {
                            $db->query("INSERT INTO `ya_kithe` (`id`,`nn`,`nd`,`na`,`n1`,`n2`,`n3`,`n4`,`n5`,`n6`,`kitm`,`kitm1`,`kizm`,`kizm1`,`zfb`,`zfbdate`, `zfbdate1`,`best`,`lx`) VALUES (1,'{$start['nq']}','{$start['kaijiang']}',0,0,0,0,0,0,0,1,'{$start['fengpan']}',1,'{$start['fengpan']}',0,'{$start['fengpan']}','{$start['kaipan']}',1,0)", 2);
                            echo "<br>【预设开奖】成功添加第" . $start['nq'] . "期";
                        } else {
                            $db->query("update `ya_kithe` SET `nn`='{$start['nq']}',`nd`='{$start['kaijiang']}',`kitm1`='{$start['fengpan']}',`kizm1`='{$start['fengpan']}',`zfbdate`='{$start['fengpan']}',`zfbdate1`='{$start['kaipan']}',`kitm`=1,`kizm`=1 where id=1 ", 2);
                            echo "<br>【预设开奖】成功修改第" . $start['nq'] . "期";
                        }
                        //第二期
                        $id2 = $db->query(" select * from `ya_kithe` where id='2'", 1);
                        if (empty($id2)) {
                            $db->query("INSERT INTO `ya_kithe` (`id`,`nn`,`nd`,`na`,`n1`,`n2`,`n3`,`n4`,`n5`,`n6`,`kitm`,`kitm1`,`kizm`,`kizm1`,`zfb`,`zfbdate`, `zfbdate1`,`best`,`lx`) VALUES (2,'{$start['nq2']}','{$start['kaijiang2']}',0,0,0,0,0,0,0,1,'{$start['fengpan2']}',1,'{$start['fengpan2']}',0,'{$start['fengpan2']}','{$start['kaipan2']}',1,0)", 2);
                            echo "<br>【预设开奖】成功添加第" . $start['nq2'] . "期";
                        } else {
                            $db->query("update `ya_kithe` SET `nn`='{$start['nq2']}',`nd`='{$start['kaijiang2']}',`kitm1`='{$start['fengpan2']}',`kizm1`='{$start['fengpan2']}',`zfbdate`='{$start['fengpan2']}',`zfbdate1`='{$start['kaipan2']}',`kitm`=1,`kizm`=1 where id=2 ", 2);
                            echo "<br>【预设开奖】成功修改第" . $start['nq2'] . "期";
                        }
                        //第三期
                        $id3 = $db->query(" select * from `ya_kithe` where id='3'", 1);
                        if (empty($id3)) {
                            $db->query("INSERT INTO `ya_kithe` (`id`,`nn`,`nd`,`na`,`n1`,`n2`,`n3`,`n4`,`n5`,`n6`,`kitm`,`kitm1`,`kizm`,`kizm1`,`zfb`,`zfbdate`, `zfbdate1`,`best`,`lx`) VALUES (3,'{$start['nq3']}','{$start['kaijiang3']}',0,0,0,0,0,0,0,1,'{$start['fengpan3']}',1,'{$start['fengpan3']}',0,'{$start['fengpan3']}','{$start['kaipan3']}',1,0)", 2);
                            echo "<br>【预设开奖】成功添加第" . $start['nq3'] . "期";
                        } else {
                            $db->query("update `ya_kithe` SET `nn`='{$start['nq3']}',`nd`='{$start['kaijiang3']}',`kitm1`='{$start['fengpan3']}',`kizm1`='{$start['fengpan3']}',`zfbdate`='{$start['fengpan3']}',`zfbdate1`='{$start['kaipan3']}',`kitm`=1,`kizm`=1 where id=3 ", 2);
                            echo "<br>【预设开奖】成功修改第" . $start['nq3'] . "期";
                        }
                        echo "获得结果</br>";
                        echo $start['qishu'] . "期：" . $start['n1'] . "-" . $start['n2'] . "-" . $start['n3'] . "-" . $start['n4'] . "-" . $start['n5'] . "-" . $start['n6'] . "-特:" . $start['na'];
                        echo "</br>开奖时间：" . $start['date'];
                        //更新开奖
                        if ($autos[0]['nn'] == $start['qishu']) {
                            //开奖第一球
                            if (!empty($start['n1']) && $start['n1'] != 0) {
                                $db->query("update `cn_kithe` SET `n1` = '{$start['n1']}' WHERE `nn` = '{$autos[0]['nn']}' LIMIT 1 ", 2);
                                $db->query("update `cn_kithe` SET `kitm` =0,`kizm` =0,`zfb` =0 WHERE `nn` = '{$autos[0]['nn']}' LIMIT 1 ", 2);
                            }

                            if (!empty($start['n2']) && $start['n2'] != 0) {
                                $db->query("update `cn_kithe` SET `n2` = '{$start['n2']}' WHERE `nn` = '{$autos[0]['nn']}' LIMIT 1 ", 2);
                                $db->query("update `cn_kithe` SET `kitm` =0,`kizm` =0,`zfb` =0 WHERE `nn` = '{$autos[0]['nn']}' LIMIT 1 ", 2);
                            }

                            if (!empty($start['n3']) && $start['n3'] != 0) {
                                $db->query("update `cn_kithe` SET `n3` = '{$start['n3']}' WHERE `nn` = '{$autos[0]['nn']}' LIMIT 1 ", 2);
                                $db->query("update `cn_kithe` SET `kitm` =0,`kizm` =0,`zfb` =0 WHERE `nn` = '{$autos[0]['nn']}' LIMIT 1 ", 2);
                            }

                            if (!empty($start['n4']) && $start['n4'] != 0) {
                                $db->query("update `cn_kithe` SET `n4` = '{$start['n4']}' WHERE `nn` = '{$autos[0]['nn']}' LIMIT 1 ", 2);
                                $db->query("update `cn_kithe` SET `kitm` =0,`kizm` =0,`zfb` =0 WHERE `nn` = '{$autos[0]['nn']}' LIMIT 1 ", 2);

                            }
                            if (!empty($start['n5']) && $start['n5'] != 0) {
                                $db->query("update `cn_kithe` SET `n5` = '{$start['n5']}' WHERE `nn` = '{$autos[0]['nn']}' LIMIT 1 ", 2);
                                $db->query("update `cn_kithe` SET `kitm` =0,`kizm` =0,`zfb` =0 WHERE `nn` = '{$autos[0]['nn']}' LIMIT 1 ", 2);
                            }

                            if (!empty($start['n6']) && $start['n6'] != 0) {
                                $db->query("update `cn_kithe` SET `n6` = '{$start['n6']}' WHERE `nn` = '{$autos[0]['nn']}' LIMIT 1 ", 2);
                                $db->query("update `cn_kithe` SET `kitm` =0,`kizm` =0,`zfb` =0 WHERE `nn` = '{$autos[0]['nn']}' LIMIT 1 ", 2);
                            }
                            if (!empty($start['na']) && $start['na'] != 0) {
                                $fa = $start['na'];
                                $fb = (int)$fa;
                                if ($fb < 10) {
                                    $vv = "0" . $fb;
                                } else {
                                    $vv = $fb;
                                }
                                $sx = Get_sx_Color($vv);

                                if ($start[0]['na'] == 0) {
                                    $db->query("update `cn_kithe` SET `na` = '{$start['na']}', `sx` = '{$sx}' WHERE `nn` = '{$autos[0]['nn']}' LIMIT 1 ", 2);
                                    $sql = "SELECT * FROM ya_kithe  order by nn LIMIT 1";
                                    $result = $db->query($sql, 1);
                                    $ykithe = $result[0];
                                    //添加新盘口
                                    $db->query("INSERT INTO `cn_kithe` (`best`, `nn`, `nd`, `kitm`, `kizm`, `zfbdate`, `kitm1`, `kizm1`, `zfbdate1`, `date`, `n1`, `n2`, `n3`, `n4`, `n5`, `n6`, `na`, `lx`) VALUES (1,'{$ykithe['nn']}','{$ykithe['nd']}','{$ykithe['kitm']}','{$ykithe['kizm']}','{$ykithe['zfbdate']}','{$ykithe['kitm1']}','{$ykithe['kizm1']}','{$ykithe['zfbdate1']}','{$ykithe['nd']}',0,0,0,0,0,0,0, 0) ", 2);
                                    echo "<br>【新盘口】成功添加第" . $ykithe['nn'] . "期";
                                    $autok = $db->query("SELECT `nn` FROM cn_kithe WHERE `n1` = '0' and `n2` = '0' and `n3` = '0' and `na` = '0'  LIMIT 1", 1);
                                    if ($start['nq'] == $autok[0]['nn']) {
                                        $nyr = date("Y-m-d", strtotime($start['kaijiang']));//年月日
                                        $db->query("UPDATE `cn_kithe` SET `nd` = '{$start['kaijiang']}',`kitm` = 1,`kizm` = 1,`zfbdate` = '{$start['fengpan']}',`kitm1` = '{$start['fengpan']}',`kizm1` = '{$start['fengpan']}',`zfbdate1` = '{$start['kaipan']}',`date` = '{$nyr}',`best` = 1 WHERE `nn` = '{$start['nq']}' LIMIT 1 ", 2);
                                        echo "<br>【新盘口】成功修改第" . $start['nq'] . "期";
                                    }
                                }
                            }
                        } else {
                            //如果没有盘口，就添加开盘信息
                            $date = $start['date'];                //开奖时间
                            $nyr = date("Y-m-d", strtotime($date));//年月日
                            $kitm1 = $nyr . ' 21:25:30';            //特码封盘时间
                            $kizm1 = $nyr . ' 21:25:30';            //正码封盘时间
                            $zfbdate = $nyr . ' 21:25:30';            //总封盘时间
                            $zfbdate1 = $nyr . ' 08:00:00';            //总开盘时间
                            $db->query("INSERT INTO `cn_kithe` (`best`, `nn`, `nd`, `kitm`, `kizm`, `zfbdate`, `kitm1`, `kizm1`, `zfbdate1`, `date`, `n1`, `n2`, `n3`, `n4`, `n5`, `n6`, `na`, `lx`) VALUES (1,'{$start['qishu']}','{$start['date']}',1,1,'{$zfbdate}','{$kitm1}','{$kizm1}','{$zfbdate1}','{$date}',0,0,0,0,0,0,0, 0) ", 2);
                            echo "<br>【新盘口】成功添加第" . $start['qishu'] . "期";
                        }
                    } else {
                        echo "等待最新开奖信息...";
                    }
                    ?>
                </b>
            </td>
        </tr>
    </table>
    <div class="footer">
        <div class="pageTitle">
            <input type=button name=button value="刷新" onClick="window.location.reload()">
        </div>
    </div>
</div><!-- wrapper end -->
</body>
</html>
<script>
    <!--
    var limit = "00:10"
    if (document.images) {
        var parselimit = limit.split(":")
        parselimit = parselimit[0] * 60 + parselimit[1] * 1
    }

    function beginrefresh() {
        if (!document.images)
            return
        if (parselimit == 1)
            window.location.reload()
        else {
            parselimit -= 1
            curmin = Math.floor(parselimit / 60)
            cursec = parselimit % 60
            if (curmin != 0)
                curtime = curmin + "分" + cursec + "秒后自动本页获取最新数据！"
            else
                curtime = cursec + "秒后自动本页获取最新数据！"
            timeinfo.innerText = curtime
            setTimeout("beginrefresh()", 5000)
        }
    }

    window.onload = beginrefresh
    -->
</script>
<span id="timeinfo"></span>
